package com.kitty.game.waiguan;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.common.utils.SpringUtils;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.codec.xf.StanUtils;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.fight.service.BroadcastService;
import com.kitty.game.function.service.FasionService;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import java.util.Map;

@Controller
public class FashionController {
    Logger logger = LoggerFactory.getLogger(FashionController.class);

    /**
     * 打开时装
     *
     * @param session
     * @param reqEnterCustom
     */
    @RequestMapping
    public void openFashion(IoSession session, ReqEnterCustom reqEnterCustom) {
        Role role = SessionUtils.getRoleBySession(session);
        FasionService fasionService = SpringUtils.getBean(FasionService.class);

        //队标
        if("team_icon".equals(reqEnterCustom.getPara())){
            fasionService.sendNullStore(session, "team_icon_store", (short) 5299, (short) 5340);
            for (Map.Entry<Integer,RoleEquip> entry :role.getEquipBox().getEquips().entrySet()) {
                RoleEquip roleEquip = entry.getValue();
                if (roleEquip.getPosition() >= 5299 && roleEquip.getPosition() <= 5340) {
                    fasionService.sendFasionStore(roleEquip.getPosition(), role, roleEquip, "team_icon_store");
                }
            }
            RespFasionTeamIconList respFasionTeamIconList = new RespFasionTeamIconList();
            respFasionTeamIconList.setList(fasionService.getFasionList(5));
            MessagePusher.pushMessage(session, respFasionTeamIconList);

            RespFasionCustomEnd respFasionCustomEnd = new RespFasionCustomEnd();
            respFasionCustomEnd.setLabel("fasion_team_icon_view");
            MessagePusher.pushMessage(session, respFasionCustomEnd);
        }else{

            fasionService.sendNullStore(session, "fasion_store", (short) 4001, (short) 4050);
            for (Map.Entry<Integer,RoleEquip> entry :role.getEquipBox().getEquips().entrySet()) {
                RoleEquip roleEquip = entry.getValue();
                if (roleEquip.getPosition() >= 4001 && roleEquip.getPosition() <= 4050) {
                    fasionService.sendFasionStore(roleEquip.getPosition(), role, roleEquip, "fasion_store");
                }
            }

            fasionService.sendNullStore(session, "custom_store", (short) 5001, (short) 5200);
            for (Map.Entry<Integer,RoleEquip> entry :role.getEquipBox().getEquips().entrySet()) {
                RoleEquip roleEquip = entry.getValue();
                if (roleEquip.getPosition() >= 5001 && roleEquip.getPosition() <= 5200) {
                    fasionService.sendFasionStore(roleEquip.getPosition(), role, roleEquip, "custom_store");
                }
            }

            fasionService.sendNullStore(session, "effect_store", (short) 4501, (short) 4510);


            //返回打开时装面板
            if (!role.isCustom()) {
                RespEnterCustom respEnterCustom = new RespEnterCustom();
                respEnterCustom.setSiver(true);
                respEnterCustom.setPara(reqEnterCustom.getPara());
                respEnterCustom.setList(fasionService.getFasionList(1));
                MessagePusher.pushMessage(session, respEnterCustom);
            } else {
                RespEnterCustom respEnterCustom = new RespEnterCustom();
                respEnterCustom.setSiver(true);
                respEnterCustom.setCustom(true);
                respEnterCustom.setPara(reqEnterCustom.getPara());
                respEnterCustom.setList(fasionService.getFasionList(3));
                MessagePusher.pushMessage(session, respEnterCustom);
            }

            RespPetFashion respPetFashion = new RespPetFashion();
            //懒得解了
            respPetFashion.setIsSiver(StanUtils.hexToByteArray("01000B5065744472657373446C67000A0CD5F2D0C7B3BDA1A43930CCEC00001AE80ECCA4D1A9D1B0C3B7A1A43930CCEC00001AE80CB5E3BAECD6F2A1A4D3C0BEC3000069080ED0C7B4B9D4C2D3BFA1A4D3C0BEC3000069080CD0FEDAA4C1D9A1A43930CCEC00001AE80EC8D5D2ABB3BDBBD4A1A4D3C0BEC3000069080ED7CFE1B0B9CAC3CEA1A43930CCEC00001AE80EBDA3C6C7C7D9D0C4A1A4D3C0BEC3000069080ED2FDCCECB3A4B8E8A1A4D3C0BEC3000069080CD0C7BBF0D5D1A1A4D3C0BEC300006908"));
            MessagePusher.pushMessage(session, respPetFashion);
        }



    }

    @RequestMapping
    public void openFashion(IoSession session, ReqRoleWaiguan reqRoleWaiguan) {
        Role role = SessionUtils.getRoleBySession(session);
        role.setCustom(reqRoleWaiguan.isCustom());
        BroadcastService broadcastService = SpringUtils.getBean(BroadcastService.class);
        FasionService fasionService = SpringUtils.getBean(FasionService.class);
        broadcastService.sendUpdateAppear(role);

        //返回打开时装面板
        if (!reqRoleWaiguan.isCustom()) {
            RespEnterCustom respEnterCustom = new RespEnterCustom();
            respEnterCustom.setList(fasionService.getFasionList(1));
            MessagePusher.pushMessage(session, respEnterCustom);
        } else {
            RespEnterCustom respEnterCustom = new RespEnterCustom();
            respEnterCustom.setSiver(false);
            respEnterCustom.setCustom(true);
            respEnterCustom.setList(fasionService.getFasionList(3));
            MessagePusher.pushMessage(session, respEnterCustom);
        }

        RespFasionCustomEnd respFasionCustomEnd = new RespFasionCustomEnd();
        respFasionCustomEnd.setLabel("swicth_label");
        MessagePusher.pushMessage(session, respFasionCustomEnd);
        role.save();
    }


    /**
     * 打开特效
     */
    @RequestMapping
    public void openEffect(IoSession session, ReqFasionEffect reqFasionEffect) {
        Role role =SessionUtils.getRoleBySession(session);
        FasionService fasionService = SpringUtils.getBean(FasionService.class);
        fasionService.sendNullStore(session, "effect_store", (short) 4501, (short) 4510);
        for (Map.Entry<Integer,RoleEquip> entry :role.getEquipBox().getEquips().entrySet()) {
            RoleEquip roleEquip = entry.getValue();
            if (roleEquip.getPosition() >= 4501 && roleEquip.getPosition() <= 4510) {
                fasionService.sendFasionStore(roleEquip.getPosition(), role, roleEquip, "effect_store");
            }
        }

        RespFasionEffectList respFasionEffectList = new RespFasionEffectList();
        respFasionEffectList.setList(fasionService.getFasionList(2));
        MessagePusher.pushMessage(session, respFasionEffectList);

        RespFasionCustomEnd respFasionCustomEnd = new RespFasionCustomEnd();
        respFasionCustomEnd.setLabel("fasion_effect_view");
        MessagePusher.pushMessage(session, respFasionCustomEnd);
    }

    @RequestMapping
    public void openFollowPetStore(IoSession session, ReqFollowPetStore reqFollowPetStore) {
        Role role =SessionUtils.getRoleBySession(session);
        FasionService fasionService = SpringUtils.getBean(FasionService.class);
        fasionService.sendNullStore(session, "follow_pet_store", (short) 5201, (short) 5250);

        for (Map.Entry<Integer,RoleEquip> entry :role.getEquipBox().getEquips().entrySet()) {
            RoleEquip roleEquip = entry.getValue();
            if (roleEquip.getPosition() >= 5201 && roleEquip.getPosition() <= 5250) {
                fasionService.sendFasionStore(roleEquip.getPosition(), role, roleEquip, "follow_pet_store");
            }
        }

        RespFollowPet respFollowPet = new RespFollowPet();
        respFollowPet.setList(fasionService.getFasionList(4));
        MessagePusher.pushMessage(session, respFollowPet);

        RespFasionCustomEnd respFasionCustomEnd = new RespFasionCustomEnd();
        respFasionCustomEnd.setLabel("view_follow_pet");
        MessagePusher.pushMessage(session, respFasionCustomEnd);

    }

    /**
     * 穿戴时装
     */
    @RequestMapping
    public void equipFasion(IoSession session, ReqFasionCustomEquip reqFasionCustomEquip) {
        SpringUtils.getBean(FasionService.class).equipFasion(session, reqFasionCustomEquip.getName());
    }

    /**
     * 卸下时装
     */
    @RequestMapping
    public void unEquipFasion(IoSession session, ReqUnEquipFasion reqUnEquipFasion) {
        Role role =SessionUtils.getRoleBySession(session);
        SpringUtils.getBean(FasionService.class).unEquipCustomFasion(role, reqUnEquipFasion.getPos(), true);
    }

    /**
     * 购买时装
     */
    @RequestMapping
    public void buyFasion(IoSession session, ReqBuyFasion reqBuyFasion) {
        SpringUtils.getBean(FasionService.class).buyFasion(session, reqBuyFasion);
    }

    /**
     * 购买跟宠
     */
    @RequestMapping
    public void buyFollowPet(IoSession session, ReqBuyFollowPet reqBuyFollowPet) {
        SpringUtils.getBean(FasionService.class).buyFollowPet(session, reqBuyFollowPet);
    }

    /**
     * 购买特效
     */
    @RequestMapping
    public void buyEffect(IoSession session, ReqBuyEffect reqBuyEffect) {
        SpringUtils.getBean(FasionService.class).buyEffect(session, reqBuyEffect);
    }
    /**
     * 购买队标
     * @param session
     * @param reqbuyFasionCustom
     */
    @RequestMapping
    public void buyFasionCustom(IoSession session, ReqBuyFasionTeamIcom reqbuyFasionCustom) {
        SpringUtils.getBean(FasionService.class).buyTeamIcon(session, reqbuyFasionCustom);
    }

}
